package com.orhon.smartcampus.modules.systemctl.mutation;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.systemctl.dao.ApisDao;
import com.orhon.smartcampus.modules.systemctl.entity.Apis;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class DeleteApiMutation implements DataFetcher {


	@Autowired
	ApisDao apisDao;

	@Override
	public Object get(DataFetchingEnvironment environment) throws Exception {
		if (environment.getArgument("id") != null) {
			List<Integer> ids = environment.getArgument("id");
			ArrayList<Apis> apis = new ArrayList<Apis>();
			for (Integer id : ids) {
				Apis item = apisDao.get(Apis.class, Long.valueOf(id));
				item.setDeletedAt(new Date());
				try {
					apisDao.update(item);
					apis.add(item);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			return Result.ok().put("data", apis);
		}
		return Result.error();
	}
}

